package projectsys.sys.core.web.controller;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;

@Controller
public class BaseController {
	
private static final Log logger = LogFactory.getLog(BaseController.class);
	
	/**
	 * 返回一个字符串，返回执行保存操作的数量结果并返回
	 * @param response
	 * @param responseContent
	 */
	protected static void flushResponse(HttpServletResponse response, String responseContent) {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");
		
		PrintWriter writer = null;
		try {
			writer = response.getWriter();
			writer.write(responseContent);
		} catch (IOException e) {
			logger.error(e);
		} finally {
			writer.flush();
			writer.close();
		}
	}
	
	/**
	 * 从request中获取参数根据键值对形成Map
	 * 数组参数只保留第一个元素，没有做分页参数设置
	 * @param request
	 * @return
	 */
	protected static Map<String, String> getParamSearchValue(HttpServletRequest request){
		Map<String, String> map = new HashMap<String, String>();
		Enumeration names = request.getParameterNames();
		while(names.hasMoreElements()) {
			String key = (String)names.nextElement();
			String value = request.getParameter(key);
			if(value != null) {
				map.put(key, value);
			}
		}
		return map;
	}
	/**
	 * <p>
	 * 从request中获取参数根据键值对形成Map. <br>
	 * 注意:对于数组参数，只拷贝了第1个元素.<br>
	 * 对于全空格的数据，仍然保留，在保存修改时可能需要.
	 * </p>
	 * 
	 * @param request
	 * @return map
	 */
	protected static Map<String, String> getParamValues(
			HttpServletRequest request) {
		Map<String, String> map = new HashMap<String, String>();
		@SuppressWarnings("rawtypes")
		Enumeration names = request.getParameterNames();
		while (names.hasMoreElements()) {
			String key = (String) names.nextElement();
			String value = request.getParameter(key);
			if (value != null ) {   //sjy 2013-05-15	&& value != ""
				map.put(key, value);
			}
		}
		String pageSizes = request.getParameter("pageSize");
		String pageNumbers = request.getParameter("pageNumber");
		String page = request.getParameter("page");
		String limit = request.getParameter("limit");
		
		int pageSize = 10;
		int pageNumber = 1;
		if (pageSizes != null && !pageSizes.equals("")) {
			pageSize = Integer.parseInt(pageSizes);
		}else if(page != null && !page.equals("")){
			pageSize = Integer.parseInt(page);
		}
		
		if (pageNumbers != null && !pageNumbers.equals("")) {
			pageNumber = Integer.parseInt(pageNumbers);
		}else if(limit != null && !limit.equals("")){ // 兼容layui分页
			pageNumber = Integer.parseInt(limit);
		}
		
		map.put("rows", String.valueOf(pageSize));
		map.put("page", String.valueOf(pageNumber));
		return map;
	}
	
	/**
	 * 从request中获取参数根据键值对形成Map，保存所有数组参数
	 * @param request
	 * @return
	 */
	protected static Map<String, String[]> getParameterValues(HttpServletRequest request){
		Map<String, String[]> paramValues = new HashMap<String, String[]>();
		Enumeration<String> names = request.getParameterNames();
		while(names.hasMoreElements()) {
			String key = (String)names.nextElement();
			String value[] = request.getParameterValues(key);
			if(value != null) {
				paramValues.put(key, value);
			}
		}
		return paramValues;
	}
	
	/**
	 * 输出照片的通用方法（依靠图片的byte信息）
	 * @param date
	 * @param response
	 */
	public void FlushPhoto(byte[] date, HttpServletResponse response) {
		InputStream in = null;
		OutputStream outStream = null;
		try {
			response.setHeader("Pragma", "No-cache");
			response.setHeader("Cache-Control", "no-cache");
			response.setDateHeader("Ecpires", 0);
			in = new ByteArrayInputStream(date);
			outStream = response.getOutputStream();
			byte[] buf = new byte[1024];
			int bytes = 0;
			while((bytes = in.read(buf)) != -1) {
				outStream.write(buf, 0, bytes);
			}
			in.close();
			outStream.flush();
			outStream.close();
			outStream = null;
			response.flushBuffer();
		} catch (Exception e) {
		}finally {
			try {
				if(in != null) {
					in.close();
				}
				if(outStream != null) {
					outStream.close();
				}
			} catch (Exception e) {
			}
		}
	}
}
